<?php

namespace App\Http\Controllers\Api\Admin\SMS;

use Bright\Aliyun\Sms\Sms;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SmsController extends Controller
{


    protected $signName = "2018静海随机派位系统";

    protected $templateCode = "SMS_132105497";//成功模板的code

    protected $sms;

    public function __construct(Sms $sms)
    {
        $this->sms = $sms;
    }

    public function sendMsg(Request $request)
    {

        $mobile = $request->get('mobile');
        $name = $request->get('name');
        $school = $request->get('school');
        $status = $request->get('status');
        $date = $request->get('date');
        $section = $request->get('section');
        $enrollSchool = $request->get('ableschool');

        if ($status == 2) {//失败
            if ($enrollSchool){/**  有尚可报名的幼儿园 */
                $TemplateParam  = [
                    'name' => $name,
                    'school' => $school,
                    'left' => $enrollSchool, /**  尚可报名的幼儿园   （静海区。。。、。。。、。。、。。幼儿园正在招生）*/
                    'time' => $date
                ];
                $this->templateCode = 'SMS_132401176';
            }else{/**  无尚可报名的幼儿园*/
                $TemplateParam  = [
                    'name' => $name,
                    'school' => $school,
                ];
                $this->templateCode = 'SMS_132396127';
            }
        }else{
            /**成功录取的短信模板*/
            $TemplateParam = [
                'name' => $name,
                'school' => $school,
                'date' => $date
            ];
        }
        $response = $this->sms->send($mobile, $this->signName, $this->templateCode, $TemplateParam);
        if ($response->Code != 'OK') {
            return response()->json(['message' => '发送失败'],481);
        }
    }
    /**
     *excel导入
     *需要列   均为字符串
     *  studentname  学生姓名
     *  zyschoolname 学校名字
     *  date         报道日期
     *  mobile       家长电话
     *  idcode       学生身份证号
     *  section      片区号
     *  ableschool   还能报名的幼儿园
     */
    public function importExcel(Request $request)
    {
        $excelFile = $request->file('file');
        $tableId = $request->get('table_id'); /**1代表导入的是录取成功的学生的excel 0代表的是录取失败的学生的excel表*/
        $extension = $excelFile->getClientOriginalExtension();
        $filePath = $excelFile->move(storage_path('app/public/excel'), 'excel.' . $extension);
        $extension = $filePath->getExtension();
        $filePath = $filePath->getPath() . '/excel.' . $extension;

        $Arr = [];
        \Excel::load($filePath, function ($reader) use (&$Arr,$tableId) {
           $data = $reader->all();
            foreach ($data as $key=>$value)
            {
                if (!empty($value->mobile)) {
                    if ($tableId == 2) {/**没有报名成功的短信模板******/
                        if ($value->ableschool){/**  有尚可报名的幼儿园 */
                            $TemplateParam  = [
                                'name' => $value->studentname,
                                'school' => $value->zyschoolname,
                                'left' => $value->ableschool, /**  尚可报名的幼儿园   （静海区。。。、。。。、。。、。。幼儿园正在招生）*/
                                'time' => $value->date
                            ];
                            $this->templateCode = 'SMS_132401176';

                        }else{/**  无尚可报名的幼儿园*/
                            $TemplateParam  = [
                                'name' => $value->studentname,
                                'school' => $value->zyschoolname
                            ];
                            $this->templateCode = 'SMS_132396127';
                        }
                    }else{
                        $TemplateParam  = [
                            /**成功录取的短信模板*/
                            'name' => $value->studentname,
                            'school' => $value->zyschoolname,
                            'date' => $value->date
                        ];
                    }

                    $response = $this->sms->send($value->mobile, $this->signName, $this->templateCode, $TemplateParam);

                    if ($response->Code != 'OK') {
                        $Arr[$key] = $this->setReturn($key, $value, $tableId);
                    }
                }
            }
        });

        if (empty($Arr)){
            return response()->json(['message' => '短信已全部发送']);
        }else{
            return $Arr;
        }
    }

    public function setReturn($key, $value, $tableId)
    {
        $returnArr = [];
        $returnArr['id'] = $key;
        $returnArr['name'] = $value->studentname;
        $returnArr['date'] = $value->date;
        $returnArr['idcode'] = $value->idcode;
        $returnArr['school'] = $value->zyschoolname;
        $returnArr['mobile'] = $value->mobile;
        $returnArr['ableschool'] = $value->ableschool;
        if ($tableId == 1){
            $returnArr['status'] = 1;
        }else{
            $returnArr['status'] = 2;
        }
        return $returnArr;
    }
}
